package com.drivers.model;

import cn.hutool.http.HttpStatus;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
/**
 * @Author:
 * @ClassName: com.drivers.model.R
 * @Date: 2024/05/01
 * @Description: Controller的统一返回值
 */
@Component
public class R extends HashMap<String, Object> {
    public R() {
        put("code", HttpStatus.HTTP_OK);
        put("msg", "success");
    }

    public static R error() {
        return error(HttpStatus.HTTP_INTERNAL_ERROR, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(HttpStatus.HTTP_INTERNAL_ERROR, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public static R ok(Object dataValue) {
        R r = new R();
        r.put("data", dataValue);
        return r;
    }

    public static R ok(String key, Object dataValue) {
        R r = new R();
        r.put(key, dataValue);
        return r;
    }

    public R put(String key, Object dataValue) {
        super.put(key, dataValue);
        return this;
    }
}
